﻿Imports ADOEntityKYC
Imports System.Data
Partial Class KYC_CDDHistory
    Inherits Web.UI.BasePage

#Region "Valiable"
    Dim ds As DataSet
    Dim LogCustEntities As LogCustEntities = New LogCustEntities
    Dim clsDataTable As New clsLINQToDataTable
    Dim convertDate As clsDateFormat
    Dim clsSessionMenuRight As clsSessionMenuRight = New clsSessionMenuRight
    Dim custSeq As Integer = 0
    Dim custType As Integer = 0
#End Region

#Region "Even Page"
    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        Try
            clsSessionMenuRight = Session("clsMenuRight")
            custSeq = Request.QueryString("CUSTSEQ")
            custType = Request.QueryString("CUSTTYPE")

            ds = Session("ds")
            If Not IsPostBack Then
                chkIsLogin()
                IsDataBind()
                CreateControlMP()
            End If
        Catch ex As Exception
            ScriptManager.RegisterStartupScript(UpdatePanel1, _
                                                            UpdatePanel1.GetType(), _
                                                            "clientScript", _
                                                            "alert('" + ex.Message.Replace("'", "") + "')", _
                                                            True)
        End Try
    End Sub

    Protected Sub Page_Unload(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Unload
        Session("ds") = ds
    End Sub
#End Region

#Region "SetForm"

    Sub CreateControlMP()
        Dim page As Label = CType(Master.FindControl("lblPage"), Label)
        page.Text = "ประวัติการประเมินความเสี่ยง"
    End Sub

#End Region

#Region "Function"

    Private Sub IsDataBind()
        Try
            Dim qryCust = (From cust In LogCustEntities.LOG_MCUSTOMER _
                      Where cust.CUSTSEQ = custSeq
                      Order By cust.LOGSEQ Descending
                      Select cust).ToList

            If Not qryCust.Count = 0 Then
                If ds.Tables.Contains("dsCustomerHis") Then
                    ds.Tables.Remove("dsCustomerHis")
                End If

                ds.Tables.Add(clsDataTable.LINQToDataTable(qryCust, "dsCustomerHis").Copy)

                If custType = 1 Then
                    lblID.Text = "เลขที่บัตร :"
                    grdIndividual.DataSource = ds.Tables("dsCustomerHis")
                    grdIndividual.DataBind()
                    grdIndividual.Visible = True
                    grdNonIndividual.Visible = False
                ElseIf custType = 2 Then
                    lblID.Text = "เลขทะเบียนนิติบุคคล :"
                    grdNonIndividual.DataSource = ds.Tables("dsCustomerHis")
                    grdNonIndividual.DataBind()
                    grdIndividual.Visible = False
                    grdNonIndividual.Visible = True
                End If

                lblCustCode.Text = qryCust(0).CUSTCODE

                lblUniqueID.Text = qryCust(0).UNIQUEID
                lblNameTH.Text = qryCust(0).THFIRSTNAME + "&nbsp;&nbsp;&nbsp;" + qryCust(0).THMIDNAME + "&nbsp;&nbsp;&nbsp;" + qryCust(0).THLASTNAME
                lblNameEN.Text = qryCust(0).ENFIRSTNAME + "&nbsp;&nbsp;&nbsp;" + qryCust(0).ENMIDNAME + "&nbsp;&nbsp;&nbsp;" + qryCust(0).ENLASTNAME
                lblNameEN.Text.ToUpper()


                clsDataTable = Nothing
            End If

        Catch ex As Exception
            Throw ex
        End Try
    End Sub

#End Region


#Region "Even GridView"

    Dim intBeforeItemIndex As Integer

    Protected Sub grdIndividual_PageIndexChanging(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewPageEventArgs) Handles grdIndividual.PageIndexChanging

        grdIndividual.PageIndex = e.NewPageIndex
        grdIndividual.SelectedIndex = -1
        grdIndividual.DataSource = ds.Tables("dsCustomerHis")
        grdIndividual.DataBind()
    End Sub
    Protected Sub grdIndividual_RowDataBound(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewRowEventArgs) Handles grdIndividual.RowDataBound

        If (e.Row.RowType.ToString = "DataRow") Then
            Dim lblUpdateDate As Label = CType(e.Row.FindControl("lblUpdateDate"), Label)
            If Not (lblUpdateDate.Text = "") Then
                convertDate = New clsDateFormat
                lblUpdateDate.Text = convertDate.chkDateThOrEn(clsDateFormat.enmDateCultureInfo.Th, lblUpdateDate.Text)
                convertDate = Nothing
            End If
        End If

    End Sub
    Protected Sub grdIndividual_SelectedIndexChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles grdIndividual.SelectedIndexChanged

        If (grdIndividual.SelectedIndex = intBeforeItemIndex) Then
            grdIndividual.SelectedIndex = -1
        End If

    End Sub
    Protected Sub grdIndividual_SelectedIndexChanging(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewSelectEventArgs) Handles grdIndividual.SelectedIndexChanging

        intBeforeItemIndex = grdIndividual.SelectedIndex

    End Sub

    Protected Sub grdNonIndividual_PageIndexChanging(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewPageEventArgs) Handles grdNonIndividual.PageIndexChanging
        grdNonIndividual.PageIndex = e.NewPageIndex
        grdNonIndividual.SelectedIndex = -1
        grdNonIndividual.DataSource = ds.Tables("dsCustomerHis")
        grdNonIndividual.DataBind()
    End Sub

    Protected Sub grdNonIndividual_RowDataBound(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewRowEventArgs) Handles grdNonIndividual.RowDataBound
        If (e.Row.RowType.ToString = "DataRow") Then
            Dim lblUpdateDate As Label = CType(e.Row.FindControl("lblUpdateDate"), Label)
            If Not (lblUpdateDate.Text = "") Then
                convertDate = New clsDateFormat
                lblUpdateDate.Text = convertDate.chkDateThOrEn(clsDateFormat.enmDateCultureInfo.Th, lblUpdateDate.Text)
                convertDate = Nothing
            End If
        End If
    End Sub

    Protected Sub grdnonIndividual_SelectedIndexChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles grdNonIndividual.SelectedIndexChanged
        If (grdNonIndividual.SelectedIndex = intBeforeItemIndex) Then
            grdNonIndividual.SelectedIndex = -1
       
        End If

    End Sub

    Protected Sub grdnonIndividual_SelectedIndexChanging(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewSelectEventArgs) Handles grdNonIndividual.SelectedIndexChanging
        intBeforeItemIndex = grdNonIndividual.SelectedIndex

    End Sub


#End Region

    Protected Sub btnClose_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnClose.Click
        ScriptManager.RegisterStartupScript(Page, Page.GetType(), "clientScript", "window.close();", True)
    End Sub
End Class
